﻿using SerieSpotter.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebMatrix.WebData;

namespace SerieSpotter.Controllers
{
    public class AdminPanelController : LanguageController
    {

        SerieSpotterDb _db = new SerieSpotterDb();
        //
        // GET: /AdminPanel/

        public ActionResult Index()
        {
            var model = _db.UserProfiles.ToList();
            return View(model);
        }

        //
        // GET: /AdminPanel/Create
        [HttpGet]
        // [Authorize(Roles = "admin")]
        [Authorize(Roles = "admin")] 
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdminPanel/Create

        [HttpPost]
        // [Authorize(Roles="admin")]
        [Authorize(Roles = "admin")]
        [ValidateAntiForgeryToken]
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {

                string strRole; 
                WebSecurity.CreateUserAndAccount(user.UserProfileUserName, user.password); // aanmaken user
                if (user.Role == true)
                {
                    strRole = "Admin";
                }
                else
                {
                    strRole = "User";
                }
                System.Web.Security.Roles.AddUserToRole(user.UserProfileUserName,strRole); // role toevoegen
                _db.SaveChanges(); // saven in db
                return RedirectToAction("Index"); // terug naar de overzichtspagina
            }
            return View(user);
        }

        //
        // GET: /AdminPanel/Edit/5
        static string role; // slaat vorige role op 
        [HttpGet]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            User user = new User();// user model
          var model = _db.UserProfiles.Find(id); // haal gegevens op
          user.UserProfileUserId = model.UserId; // gebruik user model ipv Userprofile model om ook de role te laten wijzigen
          user.UserProfileUserName = model.UserName; // user model ipv Userprofile model
            string [] roles =  System.Web.Security.Roles.GetRolesForUser(model.UserName);
            if (roles[0].Equals("Admin"))
            { 
            user.Role = true; //rol tonen
            }
            else
            {
                user.Role = false;
            }
            role = roles[0];
            return View(user); // toon op scherm 

        }

        //
        // POST: /AdminPanel/Edit/5

        [HttpPost]
        [Authorize(Roles = "admin")]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                string strRole; 
                System.Web.Security.Roles.RemoveUserFromRole(user.UserProfileUserName, role); //verwijder vorige role
                if (user.Role == true)
                {
                   strRole = "Admin";
                }
                else
                {
                    strRole = "User";
                }
                System.Web.Security.Roles.AddUserToRole(user.UserProfileUserName, strRole); // kent nieuwe role toe
                _db.SaveChanges(); // saven in db
                return RedirectToAction("Index"); // terug naar de overzichtspagina
            }
            return View(user);
        }

        //
        // GET: /AdminPanel/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /AdminPanel/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (_db != null)
                _db.Dispose();
            base.Dispose(disposing);
        }
    }
}
